package org.project.controller2;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.project.pojo.AppoimentConfirm;
import org.project.pojo.Apprecord;
import org.project.pojo.Apprecord0;
import org.project.pojo.Details2;

import org.project.pojo.Record0;
import org.project.service.user.ImgsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Controller
@RequestMapping("/confirmdetails2")
public class Thirdcontroller {
	
	public static HttpServletRequest request;   

	
	@Resource
	private ImgsService imgsService;

	

   @RequestMapping("/selectdetails2")
   @ResponseBody
   public AppoimentConfirm selectdetails2(){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	   
	   String functionNum=req.getParameter("functionNum");
	//   String arr[]=functionNum.split(",");
	   
	   String path=getLoaclURL(req)+"shell//";
	   List<Details2> list=imgsService.selectdetails2(functionNum);
	   List<Details2>  list2=new ArrayList<Details2> ();
	   if(null!=list){
		   
		   Iterator<Details2> it = list.iterator();
		   int flag=0;
			while(it.hasNext()) {
				Details2 details2=it.next();
				System.out.println(details2);
				details2.setFlag(flag);
				list2.add(details2);
				flag++;
							
			}
	   }
	   
	     System.out.println(list2);
	   AppoimentConfirm  appoimentConfirm=new AppoimentConfirm();
	   appoimentConfirm.setList(list2);
	  Record0 record0= imgsService.selectconfirmimg(functionNum);
	  appoimentConfirm.setFunctionName(record0.getFunctionName());
	  appoimentConfirm.setUrl(path+record0.getImgName());
	  
	  return appoimentConfirm;
   }
   
   
   
   
   @RequestMapping("/selectmoredetails")
   @ResponseBody
   public List<AppoimentConfirm> selectlistdetails(){
	   
	   List<AppoimentConfirm> moredetailslist=new ArrayList<AppoimentConfirm>();
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	   
	   String functionNums=req.getParameter("functionNums");
	   String arr[]=functionNums.split(",");
	   
	   for(int i=0;i<arr.length;i++){
		   String functionNum=arr[i];
		   
		   String path=getLoaclURL(req)+"shell//";
		   List<Details2> list=imgsService.selectdetails2(functionNum);
		   List<Details2>  list2=new ArrayList<Details2> ();
		   if(null!=list){
			   
			   Iterator<Details2> it = list.iterator();
			   int flag=0;
				while(it.hasNext()) {
					Details2 details2=it.next();
				//	System.out.println(details2);
					details2.setFlag(flag);
					list2.add(details2);
					flag++;
								
				}
		   }
		   
		       //  System.out.println(list2);
		   AppoimentConfirm  appoimentConfirm=new AppoimentConfirm();
		   appoimentConfirm.setList(list2);
		  Record0 record0= imgsService.selectconfirmimg(functionNum);
		  appoimentConfirm.setFunctionName(record0.getFunctionName());
		  appoimentConfirm.setUrl(path+record0.getImgName());
		   
		  moredetailslist.add(appoimentConfirm);
		   
		  		   
	   }
	   
	   
	       return moredetailslist;
	   
	   
   }
   
   
   
   
   
   
   
   
   @RequestMapping("/selectcityall")
   @ResponseBody
   public List<Apprecord> selectcityall(){
	
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

	   String cityName=req.getParameter("cityName");
	   List<Apprecord0>list=null;
	   List<Apprecord> apprecordlist=null;
  
	  /* if(null!=cityName){
		   try {
			cityName = new String(cityName.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 */
	   
	       list=imgsService.selectcityall(cityName);
	   
	    
	   
	        apprecordlist=new ArrayList<Apprecord>(); 
	   if(list!=null){
		 
		   String path=getLoaclURL(req)+"shell//";
			Iterator<Apprecord0> it = list.iterator();
			int shopid=1;
			while(it.hasNext()) {
				
				Apprecord0 apprecord0=(Apprecord0)it.next();
				Apprecord apprecord=new Apprecord();
				apprecord.setShopid(shopid);
				apprecord.setUrl(path+apprecord0.getImgname());
				apprecord.setShopName(apprecord0.getShopName());
				apprecord.setShopAddress(apprecord0.getShopAddress());
				apprecord.setStartTime(apprecord0.getStartTime());
				apprecord.setEndTime(apprecord0.getEndTime());
				apprecord.setTelephone(apprecord0.getTelephone());
				apprecord.setLatitude(apprecord0.getLatitude());
				apprecord.setLongitude(apprecord0.getLongitude());
				apprecordlist.add(apprecord);
				shopid++;
	                        	}
			}
//	   }
	        return apprecordlist;
   }
   
  
   
   
   public static String getLoaclURL(HttpServletRequest request) {

		String path_now = request.getContextPath();
		return request.getScheme() + "://" + request.getServerName() + ":"
				+ request.getServerPort() + path_now + "/";
	}
   
   
   
}
